package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author yz
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/api/trade/order")
public class ApiOrderController {
    @Autowired
    OrderService orderService;

    //1.创建订单信息
    @PostMapping("/auth/createOrder/{courseId}")
    public R createOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        //System.out.println("需要登录................");
        String memberId = JwtHelper.getId(request);
        String orderId = orderService.createOrder(courseId, memberId);
        return R.ok().data("id", orderId);
    }
    //2.获取订单信息
    @GetMapping("/auth/get/{orderId}")
    public R get(@PathVariable("orderId") String orderId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Order order=orderService.getByOrderById(orderId,memberId);
        return R.ok().data("item",order);
    }
    //3.查询课程是否购买
    @GetMapping("/auth/isBuy/{courseId}")
    public R isBuy(@PathVariable String courseId,HttpServletRequest request){
        //查询当前课程是否购买
        String memberId = JwtHelper.getId(request);
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getCourseId, courseId)
                .eq(Order::getMemberId, memberId)
                .eq(Order::getStatus, "1"));
        return R.ok().data("isBuy",order==null?"0":"1");

    }
    //查询课订单支付状态,完成支付成功跳转接口
    @GetMapping("/auth/getPayStatus/{id}")
    public R getPayStatus(@PathVariable("id") String id){
        Order order = orderService.getById(id);
        return R.ok().data("status" , order.getStatus()+"")
                .data("courseId" ,order.getCourseId() );
    }
}

